package com.body;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.connect.connect;;

public class signup  extends HttpServlet{
	
	private String password;       
	private String password2;
	private String mail;           
	private String telphone;       
	private String username;
	private int check ;            
	private Date date;
	private connect conn;         
	private Connection con;         
	private PrintWriter out;      
	private String times;           
	private String mutex = "";           
	private Statement stmt;
	private ResultSet rs;  
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		synchronized (mutex){
			 
			password = req.getParameter("user_password1");
			password2 = req.getParameter("user_password2");
			mail = req.getParameter("mail");
			telphone = req.getParameter("mmm");
			username = new String(req.getParameter("username").getBytes("ISO8859_1"),"UTF-8");
			check = Integer.parseInt(req.getParameter("dist"));
			date = new Date();
			SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd");
			times=timeFormat.format(date);
			
			conn = new connect();
			con = conn.con();
			out = resp.getWriter();
			resp.setHeader("Content-type", "text/html;charset=utf-8");
			resp.setCharacterEncoding("utf-8");
			
			try {
				stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
				if(check==0){
					if(password.length()<6 || !password.equals(password2)){
						out.print("<script>alert('Error!');history.go(-1);</script>");
						return ;
					} else {
						rs = stmt.executeQuery("SELECT * FROM user WHERE UserNumber = '"+telphone+"'");
						if(rs.next()){
							out.print("<script>alert('Cell phone number has been registered!');history.go(-1);</script>");
							return ; 
						} else {
						    signup(telphone,password,times,username,telphone,"");
						    out.print("<script>alert('Signup succeed!');</script>");
						    out.print("<script  type='text/javascript'  >; window.location.href='login.jsp';  </script>");
						}
					}
				}
				else{ 					
					if(password.length()<6 || !password.equals(password2)){
						out.print("<script>alert('Error!');history.go(-1);</script>");
						return ;
					} else {
						rs = stmt.executeQuery( "SELECT * FROM user WHERE UserNumber = '"+mail+"'");
						if(rs.next()){
							out.print("<script>alert('Email has been registered!');history.go(-1);</script>");
							return ;
						} else {
						    signup(mail,password,times,username,"",mail);
						    out.print("<script>alert('Signup succeed!');</script>");
						    out.print("<script  type='text/javascript'  >; window.location.href='login.jsp';  </script>");
						}
					}
				}
			}catch(SQLException e){
				e.printStackTrace();
			}  	
			try {                     
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
			
	private void signup(String UserNumber,String UserPassword,String Time,String UserName,String UserPhone,String UserEmail){
		try{
			stmt.executeUpdate("INSERT INTO user(UserNumber,UserPassword,Time)"
		      		+ "values('"+UserNumber+"','"+UserPassword+"','"+Time+"')");
			stmt.executeUpdate("INSERT INTO person(UserNumber,UserName,UserPhone,UserEmail,Time,Zodiac,University,Fication,Birthdate,UserSex)"
		      		+ "values('"+UserNumber+"','"+UserName+"','"+UserPhone+"','"+UserEmail+"','"+Time+"','"+""+"','"+""+"','"+""+"','"+""+"','"+""+"')");
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
}
